package descentFly.app
{
	import SEApp.se_util;
	
	import UIFree.components.uif_window;
	import UIFree.controls.uif_close;
	import UIFree.controls.uif_gbutton;
	import UIFree.controls.uif_label;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class df_window extends df_block {
		public static const SHOW: String 	= 'SHOW';
		public static const CLOSE: String 	= 'CLOSE';
		public static const TOTOP: String 	= 'TOTOP';
		
		public static var INPUTHEIGHT		: int = 30;
		public static var INPUTLABELWIDTH	: int = 90;
		
		protected var _title		: uif_label;
		protected var _close		: uif_close;
		protected var _canClose		: int;
		private var _layer			: df_block;
		private var _buttons		: df_block;
		private var _asModal		: Boolean;
		
		public function df_window(a_parent: df_modalLayer, awidth:int=0, aheight:int=0, 
								  styleClass: Object='window', 
								  modal:Boolean=false, canClose: int = 2) {
			super(a_parent, 0, 0, awidth, aheight, styleClass);
			var inner: Rectangle;
			inner = getInner();
			visible = false;
			alpha = 0;
			_canClose = canClose;
			_asModal = modal;
			_title = createTitle();
			_title.addEvent(MouseEvent.MOUSE_DOWN, function(e: MouseEvent): void {
				beginDrag(5);
			});
			
			_close = new uif_close(this);
			_close.x = inner.x + inner.width 	- _close.width;
			_close.y = inner.y;
			
			inner = getLayerRect();
			_layer = new df_block(this, inner.x, inner.y, inner.width, inner.height, style.layer);
			inner = getButtonsRect();
			if (inner)
				_buttons = new df_block(this, inner.x, inner.y, inner.width, inner.height, style.buttons);
			createDialogComponents(_layer);
			createButtons(_buttons);
			addEvent(Event.CLOSE, doClose);
			addEvent(MouseEvent.MOUSE_DOWN, function(e: MouseEvent): void {
				dispatchEvent(new Event(TOTOP));
			});
			
		}
		
		public function get parentLayer(): df_modalLayer {
			return parent.parent as df_modalLayer;
		}
		
		public function get modal(): Boolean {
			return _asModal;
		}
		
		protected function createTitle(): uif_label {
			var inner: Rectangle = getInner();
			var a_title: uif_label = new uif_label(this, inner.x, inner.y - 2, inner.width - 25, 25, style.title);
			a_title.textField.height = 25;
			return a_title;
		}
		
		protected function getLayerRect(): Rectangle {
			return getInner(0, 30, 0, -55);
		}
		
		protected function getButtonsRect(): Rectangle {
			var rect: Rectangle = getInner();
			rect.top = rect.height + rect.y - 55 + getSpace(true);
			return rect;
		}
		
		protected function createDialogComponents(a_layer: df_block): void {
		}
		
		protected function createButtons(a_layer: df_block): void {
		}
		
		protected function createInput(a_layer: uif_ecomponent, a_x: int, a_y: int, a_width: int, 
										caption: String, event: Object=null): void {
			
		}
		
		protected function createButton(a_layer: uif_ecomponent, a_x: int, a_y: int, 
										caption: String, event: Object=null): uif_gbutton {
			return se_util.defButton(a_layer, a_x, a_y, caption, (event is String)?(function(e: Event): void {
				app.dispatchEvent(new Event(event as String));
			}):(event as Function));
		}
		
		protected function addButton(caption: String, event: Object=null): void {
			if (_buttons) {
				createButton(_buttons, 0, 0, caption, event);
				_buttons.childToCenter();
			}
		}
		
		public function show(onShow: Function = null): void {
			if (!getFX('alpha')) {
				slideShow(onShow);
				dispatchEvent(new Event(SHOW));
			}
		}
		
		protected function doClose(e: Event): void {
			if (parent != null) close(canClose());
		}
		
		public function close(canCloseType: int=0, onClose: Function=null): void {
			canCloseType = canCloseType?canCloseType:_canClose;
			switch (canCloseType) {
				case 1: slideHide(function(): void {
							if (onClose != null) onClose();
						});
						break;
				case 2: slideHide(function(): void {
							if (onClose != null) onClose();
							dispose();
						});
						break;
			}
			dispatchEvent(new Event(CLOSE));
		}
		
		protected function canClose(): int {
			return _canClose;
		}
		
		public function get title(): uif_label {
			return _title;
		}
	}
}